package com.heima.article.feign;

import com.heima.article.service.ArticleService;
import com.heima.common.exception.CustomException;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-14 14:25
 */
@RestController
@RequestMapping("/api/v1/article")
public class ArticleFeign {

    @Autowired
    private ArticleService articleService;

    @PostMapping("/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto){

//        try {
//            Thread.sleep(5000L);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        if(StringUtils.isBlank(dto.getTitle())
         || StringUtils.isBlank(dto.getContent())
        ){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //保存文章 1.携带id更新 2.没有携带id新增 保存到三张表中 article config content
        Long articleId = articleService.saveArticle(dto);
        return ResponseResult.okResult(articleId);
    }
}
